home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / utilities / emulators / apple2emul.lzh / parse.c < prev    next >
C/C++ Source or Header  |  1991-04-18  |  3KB  |  222 lines

  1. /*
  2.  *  a2, an Apple II emulator in C
  3.  *  (c) Copyright 1990 by Rich Skrenta
  4.  *
  5.  *  Command line interface written by Tom Markson
  6.  *
  7.  *  Distribution agreement:
  8.  *
  9.  *    You may freely copy or redistribute this software, so long
  10.  *    as there is no profit made from its use, sale, trade or
  11.  *    reproduction.  You may not change this copyright notice,
  12.  *    and it must be included prominently in any copy made.
  13.  *
  14.  *  Send emulator related mail to:  skrenta@blekko.commodore.com
  15.  *                    skrenta@blekko.uucp
  16.  */
  17.  
  18.  
  19.  
  20. #include    <stdio.h>
  21. #include    <signal.h>
  22. #include    <ctype.h>
  23. #include    "a2.h"
  24. #include    "cli.h"
  25.  
  26.  
  27. char    *
  28. split(s)
  29. char    *s;
  30. {
  31.     char    *t;
  32.     extern char    *strchr();
  33.  
  34.     assert(s != NULL);
  35.  
  36.     t = strchr(s, ' ');
  37.     if (t == NULL)
  38.         return("");
  39.  
  40.     assert(*t == ' ');
  41.  
  42.     *t++ = '\0';
  43.  
  44.     while (*t && *t == ' ')
  45.         t++;
  46.  
  47.     return(t);
  48. }
  49.  
  50.  
  51. match(a, b)
  52. char    *a;
  53. char    *b;
  54. {
  55.  
  56.     assert(a != NULL);
  57.     assert(b != NULL);
  58.  
  59.     if (*b == '.' && strlen(b) > 1)
  60.         b++;
  61.     while (*a && *b) {
  62.         if (toupper(*a) != toupper(*b))
  63.             return(NO_MATCH);
  64.         a++;
  65.         b++;
  66.     }
  67.  
  68.     if (!*a) {
  69.         if (!*b)
  70.             return(IDENTICAL);
  71.         else
  72.             return(PARTIAL);
  73.     } else
  74.         return(NO_MATCH);
  75. }
  76.  
  77.  
  78. is_hex_number(s)
  79. char    *s;
  80. {
  81.     char    *temp;
  82.     temp = s;
  83.     assert(s != NULL);
  84.     while (*s)
  85.         if (!isxdigit(*s++))
  86.             return(FALSE);
  87.     if (temp != s)
  88.         return TRUE;
  89.     else
  90.         return FALSE;
  91. }
  92.  
  93.  
  94. long    get_hex_number(s)
  95. char    *s;
  96. {
  97.     unsigned int    x;
  98.     if (is_hex_number(s))
  99.         sscanf(s, "%x", &x);
  100.     else
  101.         return(-1);
  102.     return( (long) x );
  103. }
  104.  
  105.  
  106. parse_str(tbl, s, t)
  107. struct cmdtbl tbl[];
  108. char    *s;
  109. char    *t;            /* to be passed to handler functions */
  110. {
  111. int i;
  112. int ret;
  113. int partial = -1;
  114. int count = 0;
  115.  
  116.     i = 0;
  117.     while (tbl[i].name != NULL) {
  118.         ret = match(s, tbl[i].name);
  119.         if (ret == IDENTICAL) {
  120.             partial = i;
  121.             count = 0;
  122.             break;
  123.         } else if (ret == PARTIAL) {
  124.             partial = i;
  125.             count++;
  126.         }
  127.  
  128.         i++;
  129.     }
  130.  
  131.     if (partial == -1)
  132.         return(NO_MATCH);
  133.  
  134.     if (count > 1)
  135.         return(AMBIGUOUS);
  136.  
  137.     if (tbl[partial].func == NULL)
  138.         return(NOT_IMPLEMENTED);
  139.  
  140.     assert(t != NULL);
  141.     return  (*tbl[partial].func)(t);
  142. }
  143.  
  144.  
  145.  
  146. parse(tbl, s)
  147. struct cmdtbl tbl[];
  148. char    *s;
  149. {
  150.     int    ret;
  151.     char    buf[1024];
  152.     char    *t;
  153.  
  154.     if (*s == '!') {            /* shell escape-ish thing */
  155.         shell_escape(++s);
  156.         return(FALSE);
  157.     }
  158.  
  159.     t = split(s);
  160.  
  161.     if (*s == '\0') {
  162.         return(TRUE);                /* single step */
  163.  
  164.     } else if (strcmp(s, "?") == 0) {
  165.         dump_list(tbl, "Command, one of the following:\n\n");
  166.  
  167.     } else switch (parse_str(tbl, s, t)) {
  168.         case AMBIGUOUS:
  169.             printf("Ambiguous command '%s'.\n", s);
  170.             break;
  171.  
  172.         case NO_MATCH:
  173.             printf("Unknown command.  Type ? for a list.\n");
  174.             break;
  175.  
  176.         case NOT_IMPLEMENTED:
  177.             printf("Sorry, command not implemented yet.\n");
  178.             break;
  179.  
  180.         case OK:
  181.             break;
  182.  
  183.         case DISPLAY:
  184.             status(stdout);
  185.             break;
  186.  
  187.         default:
  188.             assert(FALSE);
  189.     }
  190.  
  191.     return(FALSE);
  192. }
  193.  
  194.  
  195. dump_list(tbl, header)
  196. struct cmdtbl tbl[];
  197. char    *header;
  198. {
  199.     int    i, count;
  200.     char    buf[1024];
  201.  
  202.     printf(header);
  203.     i = 0;
  204.     count = 0;
  205.     while (tbl[i].name != NULL) {
  206.         if (*tbl[i].name != '.' || strlen(tbl[i].name) == 1) {
  207.             if (count % 4 == 3)
  208.                 printf("    %-15s\n", tbl[i].name);
  209.             else
  210.                 printf("    %-15s", tbl[i].name);
  211.  
  212.             count++;
  213.         }
  214.         i++;
  215.     }
  216.  
  217.     if (count % 4 != 0)
  218.         printf("\n");
  219. }
  220.  
  221.  
  222.